

import time

from PyQt5 import QtCore, QtGui, QtWidgets

class TimerWidget(QtWidgets.QWidget):
    # 每秒的计时器事件
    updateSignal = QtCore.pyqtSignal()

    def __init__(self,havePassedSeconds=0):
        super(TimerWidget, self).__init__()
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.update)

        self.vbox = QtWidgets.QVBoxLayout(self)
        self.textLabel = QtWidgets.QLabel()
        self.textLabel.setText('00:00:00')
        self.vbox.addWidget(self.textLabel)
        self.havePassedSeconds = havePassedSeconds
        # self.start()

    def start(self):
        self.timer.start(1000)
        self.begin = time.time() -self.havePassedSeconds

    def update(self):
        self.d = time.time() - self.begin
        seconds = int(self.d)
        hours = int(self.d // 3600)
        mins = int((self.d % 3600) // 60)
        last = int(seconds % 60)
        fmt = '{:0>2d}:{:0>2d}:{:0>2d}'
        self.timeText = fmt.format(hours,mins,last)
        self.textLabel.setText(self.timeText)
        self.updateSignal.emit()

    def stop(self):
        if self.timer.isActive():
            self.timer.stop()
